class MySingleList {
    static class Node{
        public int val;
        public Node next;
        public Node(int val){
            this.val=val;
        }
    }
    public Node head;
    public void createLink(){
        Node node1=new Node(1);
        Node node2=new Node(2);
        Node node3=new Node(3);
        Node node4=new Node(4);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        head=node1;
    }

    public void addFirst(int data){
        Node node=new Node(data);
        node.next=head;
        head=node;
    }

    public void addLast(int data){
        Node node=new Node(data);
        if(head==null){
            head=node;
            return;
        }
        Node cur=head;
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
    }

    private Node findIndexSubOne(int index){
        Node cur=head;
        int count=0;
        while(count!=index-1){
            cur=cur.next;
            count++;
        }
        return cur;
    }
    public boolean addIndex(int index,int data){
        if(index<0||index>size()){
            return false;
        }
        if(index==0){
            addFirst(data);
            return true;
        }
        if(index==size()){
            addLast(data);
            return true;
        }
        Node cur=findIndexSubOne(index);
        Node node=cur.next;
        cur.next=node;
        return true;
    }

    public boolean contains(int key){
        Node cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    private Node searchPrev(int key){
        Node cur=head;
        while(cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }
    public void remove(int key){
        if(head==null){
            return;
        }
        if(head.val==key){
            head=head.next;
            return;
        }
        Node cur=searchPrev(key);
        if(cur==null){
            return;
        }
        Node del=cur.next;
        cur.next=del.next;
    }

    public void removeAllKey(int key){
        if(head==null){
            return;
        }
        Node prev=head;
        Node cur=head.next;
        while(cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                cur=cur.next;
            }else{
                prev=cur;
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
        }
    }

    public int size(){
        int count=0;
        Node cur=head;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    public void display(){
        Node cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    public void clear(){
        head=null;
    }
}


public class test_12_1_1{

//    public static void main(String[] args) {
//        MySingleList mySingleList=new MySingleList();
//        mySingleList.createLink();
//        mySingleList.display();
//        mySingleList.addFirst(100);
//        mySingleList.addLast(100);
//        System.out.println(mySingleList.addIndex(10, 99));
//        mySingleList.display();
//        System.out.println(mySingleList.contains(100));
//        mySingleList.remove(1);
//        mySingleList.display();
//        mySingleList.removeAllKey(100);
//        mySingleList.display();
//        System.out.println(mySingleList.size());
//        mySingleList.clear();
//        mySingleList.display();
//    }



}





//class ListNode {
//     int val;
//     ListNode next;
//     ListNode() {}
//     ListNode(int val) { this.val = val; }
//     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
//}
//class Solution {
//    public ListNode removeElements(ListNode head, int val) {
//        if(head==null){
//            return head;
//        }
//        ListNode prev=head;
//        ListNode cur=head.next;
//        while(cur!=null){
//            if(cur.val==val){
//                prev.next=cur.next;
//                cur=cur.next;
//            }else{
//                prev=cur;
//                cur=cur.next;
//            }
//        }
//        if(head.val==val){
//            head=head.next;
//        }
//        return head;
//    }
//}





